package com.livegoods.service.impl;

import com.livegoods.commons.pojo.LivegoodsResult;
import com.livegoods.pojo.Banner;
import com.livegoods.service.BannerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class BannerServiceImpl implements BannerService {

    @Autowired
    private MongoTemplate mongoTemplate;


    @Value("${livegoods.fastdfs.nginx}")
    private String nginx;

    @Override
    public LivegoodsResult banner() {

        try {
            List<Banner> banners = mongoTemplate.findAll(Banner.class);


            List<String> paths = new ArrayList<>();
            for (Banner banner : banners) {
                paths.add(nginx+banner.getPath());
            }
            LivegoodsResult ok = LivegoodsResult.ok();
            ok.setResults(paths);

            return ok;
        } catch (Exception e) {
            e.printStackTrace();
            return LivegoodsResult.error("查询广告失败....");
        }
    }
}
